#include "lucc.h"


int conf_open(conf_t *conf, char *filename)
{
	long eline;
	LHASH *lh;


	assert(conf != NULL && filename != NULL);

	if(strlen(filename) >= sizeof(conf->filename))
		return -1;
	
	lh = CONF_load(NULL, filename, &eline);
	if(lh == NULL)
		return -1;

	strcpy(conf->filename, filename);
	conf->conf = lh;

	return 0;
}

int conf_reopen(conf_t *conf)
{
	long eline;
	LHASH *lh;
	
	
	assert(conf != NULL);
	
	if(conf->conf) {
		CONF_free(conf->conf); 
		conf->conf = NULL;
	}
	
	lh = CONF_load(NULL, conf->filename, &eline);
	if(lh == NULL)
		return -1;

	conf->conf = lh;

	return 0;
}

int conf_get_int(conf_t *conf, char *item_name)
{
	char *val;
	
	assert(conf != NULL);
	
	val = CONF_get_string(conf->conf, "NULL", item_name);
	if(val == NULL)
		return -1;

	if(val[0] >= '0' && val[0] <= '9')
		return atoi(val);

	return -1;
}

char *conf_get_str(conf_t *conf, char *item_name)
{
	assert(conf != NULL);
	return CONF_get_string(conf->conf, "NULL", item_name);
}

int conf_get_int_from_section(conf_t *conf, char *section_name, char *item_name)
{
	assert(conf != NULL);
	return CONF_get_number(conf->conf, section_name, item_name);
}

char *conf_get_str_from_section(conf_t *conf, char *section_name, char *item_name)
{
	assert(conf != NULL);
	return CONF_get_string(conf->conf, section_name, item_name);
}


int conf_close(conf_t *conf)
{
	assert(conf != NULL);
	if(conf->conf)
		CONF_free(conf->conf);
	conf->conf = NULL;
	return 0;
}
